/**
 * 垃圾清理数据
 */

package com.tools.cleanmaster.model;

import com.tools.cleanmaster.model.item.TrashItem;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ouyang
 */
public class TrashesData {

    public TrashesData() {
    }

    private static boolean DEBUG = FeatureConfig.DEBUG_LOG;

    private final static String TAG = "TrashesData";

    private long mSize;

    /**
     * 获取大小
     *
     * @return
     */
    public long getSize() {
        return mSize;
    }

    private final Map<TrashType, List<TrashItem>> mTrashes = new HashMap<TrashType, List<TrashItem>>();

    public Map<TrashType, List<TrashItem>> getTrashesData() {
        return mTrashes;
    }

    /**
     * 加入垃圾
     *
     * @param t
     * @param item
     */
    public void putTrash(TrashType t, TrashItem item) {
        if (item.size <= 0) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper
                        .v(TAG, "putTrash the size of the trashItem is zero!! size = " + item.size);
            }
            return;
        }
        List<TrashItem> items = getTrashesByType(t);
        if (items == null) {
            items = new ArrayList<TrashItem>();
            mTrashes.put(t, items);
        }
        items.add(item);
        mSize += item.size;
    }

    /**
     * 获取某一类型的垃圾
     *
     * @param t
     *
     * @return 返回某一类型的垃圾
     */
    public List<TrashItem> getTrashesByType(TrashType t) {
        return mTrashes.get(t);
    }

    /**
     * 删除所有type类型
     *
     * @param type
     * @param size
     *
     * @return
     */
    public List<TrashItem> removeTrashType(TrashType type, long size) {
        List<TrashItem> removed = null;
        removed = mTrashes.get(type);
        mSize -= size;
        return removed;
    }

    /**
     * 删除某一项
     *
     * @param item
     *
     * @return
     */
    public boolean removeTrashItem(TrashItem item) {
        if (item == null) {
            return false;
        }
        List<TrashItem> list = mTrashes.get(item.trashType);
        if (list == null) {
            return false;
        }
        return list.remove(item);
    }

    /**
     * 载入默认勾选项
     */
    public void loadCheckDefault() {
        for (TrashType type : mTrashes.keySet()) {
            List<TrashItem> group = mTrashes.get(type);
            for (TrashItem trashItem : group) {
                trashItem.isSelected = trashItem.isSelectedDefault;
            }
        }
    }

    /**
     * 是否有默认勾选的选项被取消
     *
     * @return
     */
    public boolean isDefaultSelectCancel() {
        boolean isDefaultSelectCancel = false;
        for (TrashType type : mTrashes.keySet()) {
            List<TrashItem> group = mTrashes.get(type);
            for (TrashItem trashItem : group) {
                if (trashItem.isSelectedDefault && !trashItem.isSelected) {
                    isDefaultSelectCancel = true;
                    break;
                }
            }
            if (isDefaultSelectCancel) {
                break;
            }
        }
        return isDefaultSelectCancel;
    }

    /**
     * 根据不同类型获取size
     *
     * @param t
     *
     * @return
     */
    public long getSizeByType(TrashType t) {
        long totalSize = 0;
        List<TrashItem> list = getTrashesByType(t);
        if (list == null || list.isEmpty()) {
            return totalSize;
        }
        for (TrashItem item : list) {
            totalSize += item.size;
        }
        return totalSize;
    }
}
